//
//  DWLogFileForrmatter.m
//  OCAPPDemo
//
//  Created by OS on 2022/1/11.
//

#import "DWLogFileForrmatter.h"

@interface DWLogFileForrmatter ()

@property (nonatomic, strong) NSDateFormatter *df;

@end

@implementation DWLogFileForrmatter

- (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
    
    NSString *dateAndTime = [self.df stringFromDate:logMessage->_timestamp];
    
    NSString *level = [self levelStringWith:logMessage->_level];
     
    NSString *line = @"--------------------------------------";
    
    return [NSString stringWithFormat:@"%@\n%@ %@(%@) %@ %@[%zd]\n%@\n", line, dateAndTime, logMessage->_threadName, logMessage->_threadID, level, logMessage->_fileName, logMessage->_line, logMessage->_message];
    
}

- (NSString *)levelStringWith:(DDLogLevel)level {
    NSString *res = @"A";
    switch (level) {
        case DDLogLevelVerbose:
            res = @"V";
            break;
        case DDLogLevelInfo:
            res = @"I";
            break;
        case DDLogLevelError:
            res = @"E";
            break;
        case DDLogLevelDebug:
            res = @"D";
            break;
        case DDLogLevelWarning:
            res = @"W";
            break;
            
        default:
            break;
    }
    return res;
}

- (NSDateFormatter *)df {
    if (_df == nil) {
        _df = [[NSDateFormatter alloc] init];
        _df.locale = [NSLocale autoupdatingCurrentLocale];
        _df.dateFormat = @"yyyy-MM-dd_HH:mm:ss.SSS";
    }
    return _df;
}



@end
